package com.heitian.ssm.service.impl;

import com.heitian.ssm.dao.BathEntityMapper;
import com.heitian.ssm.model.BathEntity;
import com.heitian.ssm.service.BathService;
import com.heitian.ssm.utils.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: mishuai
 * Date: 2018-04-24
 * Time: 16:42
 * Des:
 */
@Service("bathService")
@Transactional(rollbackFor = Exception.class)
public class BathSerrviceImpl implements BathService {
    private Logger log = Logger.getLogger(BathSerrviceImpl.class);
    @Resource
    private BathEntityMapper bathEntityMapper;

    /**
     * 时间倒序查询洗浴情况
     * @return
     */
    public List<BathEntity> selectByCreatTime() {
        return bathEntityMapper.selectByCreatTime();
    }


    /**
     * 通过澡位id查询
     * @param bath
     * @return
     */
    public BathEntity selectByBathId(String bath) {
        BathEntity bathEntity=bathEntityMapper.selectByBathId(bath);
        return bathEntity;
    }

    public int selectByTotalByBarMonth(String chooseDate) {
        log.info("日期"+chooseDate);
        int i=0;
        try{
            i=bathEntityMapper.selectByTotalByBarMonth(chooseDate);
        }catch (Exception e){
            log.info(e);
        }

        return i;
    }


    public int selectByTotalBySexMonth(String month ,String bathRoom) {
        Map<String,String> map=new HashMap<String, String>();
        int i=0;
        try{
            map.put("month",month);
            map.put("bathRoom",bathRoom);
            i=bathEntityMapper.selectByTotalBySexMonth(map);
        }catch (Exception e){
            log.info(e);
        }

        return i;
    }

    public int selectByTotalByLineWeek(int weekpre) {
        int i=0;
        try{
            i=bathEntityMapper.selectByTotalByLineWeek(weekpre);
        }catch (Exception e){
            log.info(e);
        }
        return i;
    }

    public int insertSelective(BathEntity bathEntity) {
        log.info("入参信息bathEntity:"+ FastJsonUtils.toJSONString(bathEntity));

        if(bathEntity == null){
            return 0;
        }
        bathEntity.setBathAdmin("010110");
        log.info("bathId:"+bathEntity.getBathId());
        Integer bathId=0;
        try {

            if(bathEntity.getBathId() == null){
                return 0;
            }
            bathId =  Integer.valueOf(bathEntity.getBathId());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        int a = bathId.intValue();
        if(a<=28){
            bathEntity.setBathRoom("01");
        }else{
            bathEntity.setBathRoom("02");
        }
        bathEntity.setBathStatus("02");
        bathEntity.setCreateTime(new Date());
        bathEntity.setModifyTime(new Date());
        byte by=(byte)1;
        bathEntity.setIsStatus(by);
        int i=bathEntityMapper.insertSelective(bathEntity);
        return i;
    }


}
